package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    //添加购物车
    @PostMapping("/cart/add")
    public ResultInfo cartAdd(@RequestBody Cart cart){
        cart = cartService.cartAdd(cart);
        //前端需要从cart中获取number用于显示
        return ResultInfo.success(cart);
    }


    //获取购物车内商品的集合
    @GetMapping("/cart/list")
    public ResultInfo findCart(){
        List<Cart> cartList = cartService.findCart();
        //前端需要从cart中获取number用于显示
        return ResultInfo.success(cartList);
    }

    //删除购物车中内容
    @PostMapping("/cart/sub")
    public ResultInfo cartDelete(@RequestBody Cart cart){
        cart = cartService.cartDelete(cart);
        //前端需要从cart中获取number用于显示
        return ResultInfo.success(cart);
    }

    //清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo cartClean(){
        cartService.cartClean();
        return ResultInfo.success(null);
    }

}
